package com.keymozy.controller;

import com.keymozy.pojo.Staff;
import com.keymozy.pojo.Store;
import com.keymozy.pojo.Zone;
import com.keymozy.proxy.Right;
import com.keymozy.service.StoreService;
import com.keymozy.state.Code;
import com.keymozy.state.RespData;
import com.keymozy.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/store")
@CrossOrigin
public class StoreController {
    @Autowired
    private StoreService storeService;
    @GetMapping("/getStore")
    @Right(power = "7")
    public RespData getStore(Integer currentPage, Integer pageSize,String sname){
        log.info("进入了store");
        Map stores = storeService.getStore(currentPage,pageSize,sname);
        if (stores==null){
            return new RespData(Code.FALSE_SEARCH,null);
        }
        return new RespData(Code.SUCCESS_SEARCH,stores);
    }

    @PostMapping("/add")
    @Right(power = "7")
    public RespData addStore(@RequestBody Store store){
        Staff staff =(Staff) TokenUtil.data.get();
        if (storeService.addStore(store,staff.getId())){
            return new RespData(Code.SUCCESS_ADD_1,store);
        }
        return new RespData(Code.FALSE_ADD,null);
    }

    @PostMapping("/alter")
    @Right(power = "7")
    public RespData alterStore(@RequestBody Store store){
        log.info("store:"+store);
        if (storeService.updateStore(store))
            return new RespData(Code.SUCCESS_UPDATE_1,store);
        return new RespData(Code.FALSE_UPDATE,null);
    }

    @PostMapping("/delete")
    @Right(power = "7")
    public RespData deleteStore(@RequestBody Store store){
        if (storeService.deleteStore(store))
            return new RespData(Code.SUCCESS_DELETE_1,store);
        return new RespData(Code.FALSE_DELETE,null);
    }

    @PostMapping("/addZone")
    @Right(power = "7")
    public RespData addZone(@RequestBody Zone zone){
        if (storeService.addZone(zone))
            return new RespData(Code.SUCCESS_ADD_1,zone);
        return new RespData(Code.FALSE_ADD,null);
    }
    @PostMapping("/alterZone")
    @Right(power = "7")
    public RespData alterZone(@RequestBody Zone zone){
        if (storeService.alterZone(zone))
            return new RespData(Code.SUCCESS_UPDATE_1,zone);
        return new RespData(Code.FALSE_UPDATE,null);
    }
    @PostMapping("/deleteZone")
    @Right(power = "7")
    public RespData deleteZone(@RequestBody Zone zone){
        if (storeService.deleteZone(zone))
            return new RespData(Code.SUCCESS_DELETE_1,zone);
        return new RespData(Code.FALSE_DELETE,null);
    }
}
